﻿using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Data;

/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{
    private SqlConnection connSql;

    public WebService()
    {
        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }
    
    [WebMethod(EnableSession = true)]
    public String WSLogin(String ProviderID, String ProviderPwd)
    {
        String SID = "";
        Boolean kq = kiemtraid(ProviderID, ProviderPwd);
        if (kq == true)
        {
            SID = Session.SessionID;
            InsertSID(ProviderID, ProviderPwd);
        }
        else
        {
            SID = "-1";
        }
        return SID;
        //String SID = "123456";
        //return SID;
    }
    
    public Boolean kiemtraid(String ProviderID, String ProviderPwd)
    {
        _DataProvider.DataProvider dt = new _DataProvider.DataProvider();
        connSql = dt.connect();
        SqlDataAdapter daDepart = null;
        daDepart = new SqlDataAdapter("SELECT * FROM NhaCungCap where ProviderID = '" + ProviderID
            + "' and ProviderPwd = '" + ProviderPwd + "'", connSql);
        DataSet ds = new DataSet();
        daDepart.Fill(ds);
        if (ds.Tables[0].Rows.Count == 0)
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    
    public Boolean InsertSID(String PartnerID, String PartnerPwd)
    {
        try
        {
            DateTime date = DateTime.Now;
            String query = "INSERT INTO SID (ProviderID,SID,DateTime) "
            + "VALUES ('" + PartnerID + "','" + Session.SessionID + "','" + date + "')";
            _DataProvider.DataProvider dt = new _DataProvider.DataProvider();
            connSql = dt.connect();
            dt.executeNonQuery(query);

            return true;
        }
        catch 
        {

            return false;
        }
    }
    
    public String GetSID(String SID)
    {
        String kq = "";
        _DataProvider.DataProvider dt = new _DataProvider.DataProvider();
        connSql = dt.connect();
        SqlDataAdapter daDepart = null;
        daDepart = new SqlDataAdapter("SELECT * "
        + " FROM SID where SID ='" + SID + "' and DATEDIFF(mi,DateTime,GETDATE()) < 5", connSql);
        DataSet ds = new DataSet();
        daDepart.Fill(ds);
        if (ds.Tables[0].Rows.Count == 0)
        {
            return kq;
        }

        DataRow dr = ds.Tables[0].Rows[0];
        return dr["ProviderID"].ToString();
    }

    [WebMethod]
    public String RefuseTrans(String SID, String TransID)
    {
        try
        {

            String ParnerID = GetSID(SID);

            if (ParnerID == "")
            {
                return "-1";
            }
            else
            {
                String query = "Update GiaoDich Set State = 2 where TransID = " + TransID + "";
                _DataProvider.DataProvider dt = new _DataProvider.DataProvider();
                connSql = dt.connect();
                dt.executeNonQuery(query);
                return "0";
            }
        }
        catch
        {
            return "-2";
        }
    }

    public int GetProductIDFromTransId(String TransID)
    {
        //DataProvider dt = new DataProvider();
        //connSql = dt.connect();
        //SqlDataAdapter daDepart = null;
        //daDepart = new SqlDataAdapter("SELECT ProducID "
        //+ " FROM GiaoDich where TransID ='" + TransID + "'", connSql);
        //DataSet ds = new DataSet();
        //daDepart.Fill(ds);
        //if (ds.Tables[0].Rows.Count == 0)
        //{

        //    return kq;
        //}
        //DataRow dr = ds.Tables[0].Rows[0];
        //return dr["ProviderID"].ToString();
        try
        {
            _DataProvider.DataProvider dt = new _DataProvider.DataProvider();
            String query = "SELECT ProductID "
            + " FROM GiaoDich where TransID =" + TransID + "";

            connSql = dt.connect();
            int ProducID = (int)dt.executeScalar(query);

            return ProducID;
        }
        catch 
        {
            return -1;
        }
    }

    [WebMethod]
    public String AcceptTrans(String SID, String TransID)
    {
        try
        {
            String ParnerID = GetSID(SID);

            if (ParnerID == "")
            {
                return "-1";
            }
            else
            {
                String query = "Update GiaoDich Set State = 1 where TransID=" + TransID + "";
                _DataProvider.DataProvider dt = new _DataProvider.DataProvider();
                connSql = dt.connect();
                dt.executeNonQuery(query);
                return "0";
            }
        }
        catch
        {
            return "-2";
        }
    }
    
    public int GetProductQuantity(int ProductID)
    {
        try
        {
            int quantity = -1;
            _DataProvider.DataProvider dp = new _DataProvider.DataProvider();

            connSql = dp.connect();
            SqlDataAdapter daDepart = null;
            daDepart = new SqlDataAdapter("SELECT * "
            + " FROM SanPhamHopTac where ProductID = '" + ProductID + "'", connSql);
            DataSet ds = new DataSet();
            daDepart.Fill(ds);
            DataRow dr = ds.Tables[0].Rows[0];
            if (dr == null)
            {
                return quantity;
            }

            SqlDataAdapter daDepart1 = null;
            daDepart1 = new SqlDataAdapter("SELECT * "
            + " FROM TaiLieu where ProductID = '" + ProductID + "'", connSql);
            DataSet ds1 = new DataSet();
            daDepart1.Fill(ds1);
            DataRow dr1 = ds1.Tables[0].Rows[0];
            if (dr1 == null)
            {
                return quantity;
            }
            if (int.Parse(dr1["ProductQuantity"].ToString()) < int.Parse(dr["MinQuantity"].ToString()))
            {
                quantity = int.Parse(dr["NextTransQuantity"].ToString());
            }

            return quantity;
        }
        catch
        {
            return -1;
        }
    }

    [WebMethod]
    public int CheckInventory(String SID, String TransID)
    {
        String ParnerID = GetSID(SID);
        int soluong = -1;
        if (ParnerID == "")
        {
            return -1;
        }
        else
        {
            int ProductId = GetProductIDFromTransId(TransID);
            soluong = GetProductQuantity(ProductId);
        }

        return soluong;
    }

    [WebMethod]
    public int UpdateInventory(String SID, String TransID, int AddedQuantity, float PricePerUnit, float TransportPrice, float TotalPrice, String LinkTrans)
    {
        try
        {
            String ParnerID = GetSID(SID);
            //int soluong = -1;
            if (ParnerID == "")
            {
                return -1;
            }
            else
            {
                Boolean kq = InsertCTGiaoDich(SID, TransID, AddedQuantity, PricePerUnit, TransportPrice, TotalPrice, LinkTrans);
                if (kq == true)
                {
                    return 0;
                }
                else
                {
                    return -1;
                }
            }
        }
        catch
        {
            return -2;
        }
    }


    public Boolean InsertCTGiaoDich(String SID, String TransID, int AddedQuantity, float PricePerUnit, float TransportPrice, float TotalPrice, String LinkTrans)
    {
        try
        {
            int ProductId = GetProductIDFromTransId(TransID);
            _DataProvider.DataProvider dt = new _DataProvider.DataProvider();
            String query = "INSERT INTO CTGiaoDich(TransID,SessionID,ProductID,"
            + " AddedDate,AddedQuantity,PricePerUnit,TransportPrice,TotalPrice,LinkTransport,State)"
            + " Values (" + TransID + ",'" + SID + "'," + ProductId + "," + DateTime.Now.ToShortDateString() + ","
            + "'" + AddedQuantity + "'," + PricePerUnit + "," + TransportPrice + "," + TotalPrice + ","
            + "'" + LinkTrans + "','0')";
            connSql = dt.connect();
            dt.executeNonQuery(query);

            return true;
        }
        catch 
        {
            return false;
        }
    }
}